package slm.games.tris;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.util.Log;

public class ImageAdapter extends BaseAdapter {
     
    
    private SquareImageView [] imageViews = null;
    
    static final int BLANK= 0;
    static final int CROSS= 1;
    static final int CIRCLE= 2;
    
   
    public ImageAdapter(Context c) {
    	super();
    	Log.d(MainActivity.TAG,"Called Image Adapter!");
    	//Toast.makeText(c, "Called Image Adapter!" , Toast.LENGTH_LONG).show();
    	
        this.setupImages(c);
    }
 
    private void setupImages(Context c)
    {   
    	Log.d(MainActivity.TAG,"CLEARING IMAGES");
    	//Toast.makeText(this.mContext, "CLEARING IMAGES!" , Toast.LENGTH_SHORT).show();
    	if (this.imageViews==null)
    		this.imageViews = new SquareImageView[9];
    		
    	for (int i=0;i<this.imageViews.length;i++)
    	{
    	 if (this.imageViews[i]==null)
    		{
    			this.imageViews[i] = new SquareImageView(c);
    			//this.imageViews[i].setLayoutParams(new SquareGridView.LayoutParams(80, 80));
    			this.imageViews[i].setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    			//this.imageViews[i].setPadding(10,10,10, 10);
    			
    		}
    		
    		this.setToBlank(i);
    	}
    }
    
    public int getCount() {
        return this.imageViews.length;  
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    	//Log.d(MainActivity.TAG,"Called getView on POS:" + position);
 
        return this.imageViews[position];
    }
   
    private boolean setDrawableToCell(int res_id, int position)
    {
    	if ( position<0 || position>=imageViews.length)
    		return false;
    	else
    	{
    		this.imageViews[position].setImageResource(res_id);
    		return true;
    	}
    }
    
     
    public void setToCross(int position)
    {
    	this.setDrawableToCell(R.drawable.tris_x, position);	 
    }
    
    public void setToCircle(int position)
    {
  
    	this.setDrawableToCell(R.drawable.tris_o, position);
    }
    
    public void setToBlank(int position)
    {
    	 this.setDrawableToCell(R.drawable.tris_blank, position);
    }
    
    public void setAllToBlank()
    {
    	for (int i=0;i<this.imageViews.length;i++)
    	{
    		this.setToBlank(i);
    	}
    	
    }
     

	 
}